package com.example.robertluozizhao.framecollectdemo.adapter;

import android.content.Context;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;

import java.util.List;

/**
 * @author luo_zi_zhao
 * @version V1.0 当前版本描述信息
 * @FileName com.aebell.app.sxjs.view.adapter
 * @date 2019/4/24 15:29
 */

public class TabPagerAdapter extends FragmentPagerAdapter {
    private String tabTitles[] = new String[]{};

    private Context context;
    private List<Fragment> mFragmentList;

    public TabPagerAdapter(FragmentManager fm, Context context, List<Fragment> fragments, String[] tabTitles) {
        super(fm);
        this.context = context;
        this.mFragmentList = fragments;
        this.tabTitles = tabTitles;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

    @Override
    public Fragment getItem(int position) {
        if (mFragmentList != null && position < mFragmentList.size()) {
            return mFragmentList.get(position);
        }
        return null;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }


    //注意！！！这里就是我们自定义的布局tab_item
    public View getCustomView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_item_sx_wp_home, null);
        ImageView iv = (ImageView) view.findViewById(R.id.tab_item_iv);
        TextView tv = (TextView) view.findViewById(R.id.tab_item_tv);
        switch (position) {
            case 0:
                iv.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_ball));
                tv.setText(tabTitles[0]);
                break;
            case 1:
                iv.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_android));
                tv.setText(tabTitles[1]);
                break;
            case 2:
                iv.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_head));
                tv.setText(tabTitles[2]);
                break;
        }
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
    }
}